'use strict';

var Category = require('../model/category.js');
var redisCo = require('../model/redisCo.js');
var debug = require('debug')('category');

function* save(cateList) {
	debug(cateList[0]);
	try {
		var count = 0;
		// cateList.forEach(function(aCate) {
		// 	yield aCate.save();
		// 	count++;
		// });

		for (var i in cateList) {
			debug('---save category---');
			debug(cateList[i]);
			yield cateList[i].save();
			count++;
		}
		return 'save success, count=' + count;
	} catch (e) {
		return e.toString();
	}
}

function* findAll() {
	var result = yield Category.find({}).exec();
	return result;
}

function* findById(id) {
	console.log('findById, id=' + id);
	try {
		var result = yield Category.find({
			_id: id
		}).exec();
		return result;
	} catch (e) {
		return e.toString();
	}
}

function* findByName(name) {
	console.log('findByName, name=' + name);
	try {
		var result = yield Category.find({
			name: name
		}).exec();
		console.log(result);
		return result;
	} catch (e) {
		return e.toString();
	}
}

module.exports = {
	save: save,
	findAll: findAll,
	findById: findById,
	findByName: findByName,
};